/**
 * Created with IntelliJ IDEA.
 * User: teddy zhang
 * Date: 2015-04-17
 * Time: 11:25
 */

define(function (require, exports, module) {
    'use strict';
    
    var rk = require('rk');

    $.widget('rk.usersettingPwdCtrl', {
        options: {
            selector: null
        },

        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._render();
            me._bind();
        },

        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._passwordRuleRender();
        },

        _passwordRuleRender: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var ruleUrl = '/json/oa_profile-edit/personal-password-rule.action';
            $.postJson(ruleUrl).done(function (json) {
                if (json.status == 0) {
                    var data = json.data;

                    if (null != data.containRule && 0 == data.containRule) {
                        elem.find('span.p-passwordRule-content').html("密码规则："+data.passwordLength+"-16位，可以包含数字、大小写字母和特殊字符。").show();
                    }else if (null != data.containRule && 1 == data.containRule) {
                        elem.find('span.p-passwordRule-content').html("密码规则："+data.passwordLength+"-16位，必须大小写字母,可以包含数字和特殊字符。").show();
                    }else if (null != data.containRule && 2 == data.containRule) {
                        elem.find('span.p-passwordRule-content').html("密码规则："+data.passwordLength+"-16位，必须包含数字、大小写字母,可以包含特殊字符。").show();
                    }else if (null != data.containRule && 3 == data.containRule) {
                        elem.find('span.p-passwordRule-content').html("密码规则："+data.passwordLength+"-16位，必须包含数字、大小写字母和特殊字符。").show();
                    };

                    if (null != data.diffPassportCount && 0 != data.diffPassportCount) {
                        elem.find('span.p-passwordRule-content').html(elem.find('span.p-passwordRule-content').html()+'不可与最近' + data.diffPassportCount + '个密码重复。').show();
                    };
                } else {
                    rk.noticeError("获取密码规则失败");
                }

            });
        },

        _render: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var pwd_HTML = require('page/tmpl/usersetting/tpl_user_setting_pwd.tpl');
            $(elem).html(rk.templateText(pwd_HTML));
        },

        _bind: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;


            $(elem).on('click', 'a[act="pwd_btn_save"]', function (event) {
                event.preventDefault();
                var $this = $(this);
                if (!me._validate())  return;


                var oldPwd = $(elem).find('.password-edit').find('input[data-name="oldPwd"]').val();
                var newPwd = $(elem).find('.password-edit').find('input[data-name="newPwd"]').val();
                var updateUrl = '/json/oa_profile-edit/passwd-update.action';
                if (SESSION.pcCode == 100) {
                    updateUrl = '/focusmedia/custom/password-update.action';
                }
                $.postJson(updateUrl, {
                    oldPw: oldPwd,
                    newPw: newPwd
                }).done(function (json) {
                    if (json.status == 0) {
                        rk.noticeSuccess(rk.i18n('USERSET_PASSWORD_UPDATED'));
                        me._reset();
                    } else if (json.status == 507) {
                        rk.noticeError(rk.i18n('USERSET_OLD_PASSWORD_NOT_CORRECT'));
                        me._reset();
                    } else if (json.status == 100015) {
                        rk.noticeError(json.data);
                        me._reset();
                    } else {
                        rk.noticeError(rk.i18n('USERSET_PASSWORD_UPDATE_FAILED'));
                    }

                });
                return false;

            });

        },

        _reset: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            opt.selector.trigger('click');

        },

        _validate: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var _passwordFormat = new RegExp(/^[a-zA-Z0-9\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\uFF01\u201c\u201d\u2018\u2019\u300e\u300f\u300c\u300d\uFF09\uFF08\.\_\-\?\~\!\@\#\$\%\^\&\*\\\+\`\=\[\]\(\)\{\}\|\;\'\:\"\,\/\<\>]+$/);

            var oldPwd = $(elem).find('.password-edit').find('input[data-name="oldPwd"]').val();
            var isValid = true;
            if ($.trim(oldPwd) === '') {
                $(elem).find('span[data-name="oldPwd"]').text(rk.i18n('USERSET_OLD_PASSWORD_IS_MANDATORY'));
                isValid = false;
            } else {
                $(elem).find('span[data-name="oldPwd"]').text("");
                isValid = true;
            }
            var newPwd = $(elem).find('.password-edit').find('input[data-name="newPwd"]').val();
            if (!_passwordFormat.test(newPwd) || newPwd.length < 6) {
                $(elem).find('span[data-name="newPwd"]').text(rk.i18n('USERSET_INTRODUCE_CORRECT_PASSWORD'));
                isValid = false;
            } else {
                $(elem).find('span[data-name="newPwd"]').text("");
                isValid = true;
            }
            var newPwdAgain = $(elem).find('.password-edit').find('input[data-name="newPwdAgain"]').val();
            if (!_passwordFormat.test(newPwdAgain) || newPwdAgain.length < 6) {
                $(elem).find('span[data-name="newPwdAgain"]').text(rk.i18n('USERSET_INTRODUCE_CORRECT_PASSWORD'));
                isValid = false;
            } else {
                if (newPwd !== newPwdAgain) {
                    $(elem).find('span[data-name="newPwdAgain"]').text(rk.i18n('USERSET_TWO_PASSWORDS_NOT_MATCH'));
                    isValid = false;
                } else {
                    $(elem).find('span[data-name="newPwdAgain"]').text("");
                    isValid = true;
                }

            }

            return isValid;
        }


    });

    return $;

});

